# 导入 requests 包
import requests

def doGet1():
    # 发送请求
    x = requests.get('https://www.runoob.com/')

    # 返回网页内容
    print(x.text)


def doGet2():
    kw = {'s': 'python 教程'}

    # 设置请求头
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}

    # params 接收一个字典或者字符串的查询参数，字典类型自动转换为url编码，不需要urlencode()
    response = requests.get("https://www.runoob.com/", params=kw, headers=headers)

    # 查看响应状态码
    print(response.status_code)

    # 查看响应头部字符编码
    print(response.encoding)

    # 查看完整url地址
    print(response.url)

    # 查看响应内容，response.text 返回的是Unicode格式的数据
    print(response.text)




def doPost1():
    # 发送请求
    x = requests.post('https://www.runoob.com/try/ajax/demo_post.php')

    # 返回网页内容
    print(x.text)

def doPost2():
    # 表单参数，参数名为 fname 和 lname
    myobj = {'fname': 'RUNOOB', 'lname': 'Boy'}

    # 发送请求
    x = requests.post('https://www.runoob.com/try/ajax/demo_post2.php', data=myobj)

    # 返回网页内容
    print(x.text)




def weather_app():
    api_key = 'your_api_key'
    city = input("输入城市名称：")
    url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'

    response = requests.get(url)
    data = response.json()

    if data['cod'] == 200:
        main = data['main']
        temperature = main['temp']
        weather = data['weather'][0]['description']
        print(f"温度：{temperature}")
        print(f"天气：{weather}")
    else:
        print("城市未找到")


# doGet1()
# doGet2()
#
# doPost1()
# doPost2()

weather_app()